//
//  TextInterface.cpp
//  Dungeons and Princesses
//
//  Created by John Grange on 10/22/11.
//  Copyright (c) 2011 SD Networks. All rights reserved.
//

#include <iostream>
#include "TextInterface.h"
#include <string>
#include "Utility.h"
#include <algorithm>
#include "Monster.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <iomanip>
using std::cin;
using std::cout;
using std::string;
using std::endl;

TextInterface::TextInterface(Game *ptrToCurrentGame) : Interface(ptrToCurrentGame) {
    
    std::cout << VERSION << endl;
    std::cout << WELCOMEMSG << endl;
    numberOfQuitRequests = 0;
}


void TextInterface::loadInterface() {
    
    showHelp(gamePtr->loadHelp());
    
    if (!gamePtr->getCurrentPlayer()->isDead()) {
        update("");
        while (true) {
            if (!gamePtr->getCurrentPlayer()->isDead()) {
                showPrompt();
            }
            else {
                break;
            }
        }
        
    }
    
    
    
}
bool TextInterface::getResponse(int &choice) {
    
    char str1[30];
    cin.getline(str1,30);
    if (Utility::isStringAllDigits(str1))
    {
        choice = atoi (str1);
        if (choice > 3)
        {
            cout << "your number is too big. Please enter again" << endl;
            return false;
        }
        else
        {
            return true;
        }
        
    }
    else
    {
        cout << "Your enter is not a number. Please enter again " << endl;
        return false;
    }
    
    
}
void TextInterface::update(string errorMSG) {
    
    if (errorMSG != "") {
        cout << endl << errorMSG << endl << endl;
    }
    showDescription();
    
    
    if (gamePtr->getCurrentRoom()->getListOfMonsters()->size() > 0) {
        enterBattle();
    }
    else {
        
        showRoomInventory(gamePtr->getCurrentRoom()->getManifest());
        //showPrompt();
    }
}

int TextInterface::loadGame() {
    
    bool questionAnswered = false;
    
    while (!questionAnswered) {
        
        
        cout << "\nDo you wish to (l)oad or start a (n)ew game? " << std::flush;
        
        string answer;
        std::getline (cin,answer);
        bool isSavedGame;
        if (answer == "l" || answer == "L") {
            vector<string> gameList = gamePtr->getGameList(isSavedGame);
            if(isSavedGame)
            {                for(int i = 0; i < gameList.size(); ++i)
            {
                cout << i + 1 << ") " << gameList.at(i) << endl;
            }
                cout << endl << endl;
                getResponse("\nChoose a game to load: ", answer);
                
                if(find_if(answer.begin(), answer.end(), Utility::charIsNotNumeric) == answer.end())
                {
                    int responseIndex = atoi(answer.c_str());
                    if(responseIndex > 0 && responseIndex <= gameList.size())
                    {
                        return responseIndex - 1;
                    }
                }
            }
            else
            {
                printErrorMsg("\nThere are no saved Games!");
            }
        }
        else if (answer == "n" || answer == "N") {
            questionAnswered = true;
            return -1;
        }
        else {
            std::cout << "Incorrect! Please enter (l)oad or (n)ew " << std::endl;
        }
    }
    return -1;
}

void TextInterface::setupPlayer() {
    
    std::string errorMSG = "";
    bool playerIsSetup = false;
    
    while (!playerIsSetup) {
        if (errorMSG != "") {
            cout << endl << errorMSG << endl << endl;
        }
        
        
        string playerName = "";
        while(playerName.compare("") == 0)
        {
            std::cout << "\nPlease enter your character name: " << flush;
            getline(cin, playerName);
        }
        
        playerName[0] = toupper(playerName[0]);
        
        std::cout << "\nPlease enter your Character Class: " << endl;
        std::cout << "1) warrior" << endl;
        std::cout << "2) wizard" << endl;
        std::cout << "3) ranger" << endl;
        std::cout << ": " << flush;
        
        string playerClass;
        getline(cin, playerClass);
        
        playerClass = Utility::lowerCase(playerClass);
        
        playerIsSetup = gamePtr->setupPlayer(playerName, playerClass, errorMSG);
        
    }
    
}

void TextInterface::updateBattleHud(string messageToPlayer) {
    
    std::cout << messageToPlayer;
    
    std::cout << endl << endl;
    
    //std::cout << "***************** | Your HP: " << gamePtr->getCurrentPlayer()->getPlayerStats().getHitPoints() << " - Monster HP: " << currentMonster->getMonsterStats().getHitPoints() << " | *******************" << endl;
    //std::cout << "***************** | Your AP: " << gamePtr->getCurrentPlayer()->getPlayerStats().getActionPoints() << " - Monster AP: " << currentMonster->getMonsterStats().getActionPoints() << " | *******************" << endl;
    
}
void TextInterface::showRoomInventory(const vector<Listing> * roomInv) {
    
    if (roomInv->size() > 0) {
        std::cout << "\n" << "In the room you see the following : " << endl;
        for_each(roomInv->begin(), roomInv->end(), Utility::printListingOBject);
        
        std::cout << endl;
    }
}

void TextInterface::showDescription() {
    string desc = getCurrentRoomDescription();
    if(desc.compare("null") != 0)
    {
        cout << endl << endl << desc;
    }
}

void TextInterface::showPrompt() {
    
    string command;
    cout << endl << "------------------------------------" << endl << endl
    << gamePtr->getCurrentPlayer()->getDesc()
    << ", what do you wish to do ?   ";
    getline (cin,command);
    
    executeCommand(command);
    
}

bool TextInterface::showPlayerInventory(const vector<Listing> * playerInv, std::string &error) {
    
    
    if (playerInv->size() > 0) {
        std::cout << "\n" << "You have in your inventory the following items : " << endl << endl;
        for_each(playerInv->begin(), playerInv->end(), Utility::printListingOBject);
        
        std::cout << endl;
        
        return true;
    }
    
    else {
        error = "You do not have anything in your inventory";
        return false;
    }
}

void TextInterface::showHelp(vector<std::string> *helpFile) {
    
    vector<std::string>::iterator helpLine;
    
    std::cout << endl << endl;
    for (helpLine = helpFile->begin(); helpLine != helpFile->end(); helpLine++) {
        std::cout << *helpLine << endl;
    }
    
    //update();
}

void TextInterface::getResponse(std::string prompt, std::string &response) {
    
    
    cout << prompt << flush;
    getline(cin, response);
    
    Utility::removeGarbage(response);
    Utility::lowerCase(response);
    
}


bool TextInterface::attackMonster(Player * currentPlayer, Monster * currentMonster) {
    int attackToUse;
    std::string message;
    const vector<AttackType>* attacks = currentPlayer->getAttacks();
    char str[30];
    bool enter = true;
    do
    {
        cout << endl << "What attack would you like to use? " << endl;
        for (int i = 0; i < attacks->size(); i++)
        {
            cout << "*  "<< i + 1 << ") " << attacks->at(i).getDesc() << endl;
        }
        cout << "*  ";
        cin.getline(str,30);
        if (Utility::isStringAllDigits(str))
        {
            attackToUse = atoi (str);
            if (attackToUse > attacks->size() || attackToUse == 0)
            {
                cout << "your number is too big. Please enter again" << endl;
                
            }
            else
            {
                enter = false;
            }
        }
        else
        {
            cout << "Your enter is not a number. Please enter again " << endl;
        }
    }while (enter);
    
    int hitpointMonster = currentMonster->getMonsterStats().getHitPoints();
    int hitpointPlayer = currentPlayer->getPlayerStats().getHitPoints();
    cout << "****************************************************************" << endl;
    updateBattleHud("");
    
    cout << "***********************************************************************" << endl;
    cout << "*  You attack the monster" << endl;
    
    message = Attack::doAttack(currentPlayer, currentMonster, attacks->at(attackToUse -1));
    
    hitpointMonster = currentMonster->getMonsterStats().getHitPoints();
    
    std::cout << "   " << message << endl;
    std::cout << "*  Your HP: " << hitpointPlayer << " - Monter HP: " << hitpointMonster << endl;
    if (currentMonster->isDead()) {

        cout << "*  Monster has died!" << endl;
        cout << "************************************************************************" << endl;
        return true;;
        
    }
    else {
        return false;
    }
    
}
bool TextInterface::attackPlayer(Player * currentPlayer, Monster * currentMonster) {
    std::string message;
    int hitpointMonster = currentMonster->getMonsterStats().getHitPoints();
    int hitpointPlayer = currentPlayer->getPlayerStats().getHitPoints();
    
    cout << endl << endl;
    cout << "*  The Monster's turn to attack" << endl;;
    
    message = Attack::doAttack(currentMonster, currentPlayer);
    
    hitpointPlayer = currentPlayer->getPlayerStats().getHitPoints();
    std::cout << "   " << message << endl;
    std::cout << "*  Your HP: " << hitpointPlayer << " - Monter HP: " << hitpointMonster << endl;
    if ( currentPlayer->isDead()) {
        
        cout << "*  You are dead!" << endl;
        cout << "************************************************************************" << endl;
        return true;
        
    }
    cout <<"************************************************************************" << endl;
    return false;
    
}

bool TextInterface::pickUp(string itemToPickUp)
{
    int count = 0;
    string des;
    long found;
    int objectID[10];
    string objectType[10];
    
    vector<Listing>const * roomInventory = gamePtr->getCurrentRoom()->getManifest();
    
    vector<Listing> * itemsFound = new vector<Listing>;
    
    for (int index = 0; index < roomInventory->size(); index++)
    {
        des = roomInventory->at(index).description;
        
        des = Utility::lowerCase(des);
        
        found = des.find(Utility::removeGarbage(itemToPickUp));
        
        //found = des.find("Rusty");
        if (found != string::npos)
        {
            Listing itemToAdd = roomInventory->at(index);
            
            itemsFound->push_back(itemToAdd);
            
            objectID[count] = roomInventory->at(index).objectID;
            objectType[count] = roomInventory->at(index).objectType;
            count++;
        }
    }
    
    if (count == 1) {
        
        string response = getObject(objectType[0], objectID[0]);
        
        update(response);
        return true;
    }
    else if( count > 1 ) {
        
        bool validChoice;
        int choice;
        
        while (!validChoice) {
            std::cout << "Which item do you want?" << endl;
            Utility::printVectorList(itemsFound);
            std::cout << ": " << flush;
            
           
            
            validChoice = getResponse(choice);

        }
        
        string response = getObject(itemsFound->at(choice).objectType, itemsFound->at(choice).objectID);
        
        update(response);
        return true;
        
    }
    else if( count < 1 )
    {
        update("There is no such object in this room, try again");
        return false;
    }
    return true;
}


void TextInterface::enterBattle() {
    
    
#warning Maybe we should pass in the list of monsters? Im not sure yet.
    
    const vector<Monster*> * listOfMonsters = gamePtr->getCurrentRoom()->getListOfMonsters();
    gamePtr->getCurrentRoom()->setVisited();
    int monsterToAttack;
    int initialxp = gamePtr->getCurrentPlayer()->getPlayerStats().getExp();
    int initiallevel = gamePtr->getCurrentPlayer()->getPlayerStats().getLevel();
    
    if (listOfMonsters->size() > 0)
    {
        cout << endl << endl << "********************************Encounter time!***********************************" << endl;
        cout << "You are ambushed by the following creatures:" << endl << endl;
        
        for (int index = 0  ; index < listOfMonsters->size(); index++)
        {
            cout << "\t" << index + 1 << ") " << listOfMonsters->at(index)->getDesc() << " (Level " <<
            		listOfMonsters->at(index)->getMonsterStats().getLevel() << ")\n";
        }
        cout << endl << "Which Monster do you want to attack, please enter number: " << flush;
        cin >> monsterToAttack;
        cin.ignore();
        
    }
    currentMonster = listOfMonsters->at(monsterToAttack -1);
    
    int hitpointMonster = currentMonster->getMonsterStats().getHitPoints();
    //int damageMonster = hitpointMonster;
    int hitpointPlayer = gamePtr->getCurrentPlayer()->getPlayerStats().getHitPoints();
    //int damagePlayer = hitpointPlayer;
    
    int newAction;
    bool monsterDead = false;
    bool playerDead = false;
    std::string message;
    bool breakOut = false;
    while ( !monsterDead && !playerDead && !breakOut ) {
        
        // Players turn starts here ********
        if (!(gamePtr->getCurrentPlayer()->isDead() && listOfMonsters->at(monsterToAttack -1)->isDead()))
        {
            char str1[30];
            bool flag = true;
            do{
            	//cout << "\n****************************************************************" << endl;
            	cout << "************ | Your HP: " << setw(4) << gamePtr->getCurrentPlayer()->getPlayerStats().getHitPoints() << "  -  Monster HP: " << setw(4) << currentMonster->getMonsterStats().getHitPoints() << " | *************" << endl;
                cout << "************ | Your AP: " << setw(4) << gamePtr->getCurrentPlayer()->getPlayerStats().getActionPoints() << "  -  Monster AP: " << setw(4) << currentMonster->getMonsterStats().getActionPoints() << " | *************" << endl;
                cout << "*****************************************************************" << endl;
                cout << "*  What action : " << endl;
                cout << "*  1) ATTACK" << endl;
                cout << "*  2) RUN" << endl;
                cout << "*  3) OTHERS" << endl;
                cout << "*  What action do you want ? (1, or 2 or 3) " << endl;
                cout << "*  " ;
                cin.getline(str1,30);
                if (Utility::isStringAllDigits(str1))
                {
                    newAction = atoi (str1);
                    if (newAction > 3)
                    {
                        cout << "your number is too big. Please enter again" << endl;
                    }
                    else
                    {
                        flag = false;
                    }
                    
                }
                else
                {
                    cout << "Your enter is not a number. Please enter again " << endl;
                }
                
            }while (flag);
            //cout << "****************************************************************" << endl;
            switch (newAction) {
                case 1:{
                    
                    // Players turn ends here *****
                    if (attackMonster(gamePtr->getCurrentPlayer(), currentMonster)) {
                        monsterDead = true;
                        break;
                    }
                    // Monsters turn starts here ****
                    if (attackPlayer(gamePtr->getCurrentPlayer(), currentMonster)) {
                        playerDead = true;
                        break;
                    }
                    // ****** Monsters turn ends here
                    break;
                }
                case 2: {
                    
                    Direction * directions = gamePtr->getCurrentRoom()->getDirections();
                    
                    vector<Direction> * directionsToGo = new vector<Direction>;
                    for(int i = 0; i < 4; i++)
                    {
                        if (directions[i].getHasDoor() && !directions[i].getDoor()->isLocked()) {
                            directionsToGo->push_back(directions[i]);
                        }
                        else if (!directions[i].getHasDoor() && directions[i].getRoomID() != "null") {
                            directionsToGo->push_back(directions[i]);
                        }
                    }
                    
                    if (directionsToGo->size() > 0) {
                        int indexOfDirectionToGo = Utility::getSeed((int)directionsToGo->size());
                        
                        if (Attack::run(gamePtr->getCurrentPlayer(), currentMonster)) {
                            gamePtr->setCurrentRoom(gamePtr->getCastle()->getRoom(directionsToGo->at(indexOfDirectionToGo).getRoomID()));
                            cout << "^^^^^^^^^^^^^^^You cowardly escape the battle and run blindly^^^^^^^^^^^^^^^^" << endl;
                            breakOut = true;
                            
                        }
                        else {
                            
                            cout << "*********Sorry**********You failed to run******The Monster grabbed you**********. " << endl;
                            if (attackPlayer(gamePtr->getCurrentPlayer(), currentMonster)) {
                                playerDead = true;
                                break;
                            }
                        }
                        break;
                        
                    }
                    else {
                        cout << "*********Sorry**********You failed to run******All Doors are locked**********. " << endl;
                        break;
                    }
                    break;
                }
                case 3: {
                    
                    
                    string command;
                    string error;
                    
                    cout << "Your wish is my command ? " << flush;
                    getline (cin,command);
                    
                    executeBattleCommand(command, error);
                    
                    cout << error << endl;
                    
                    
                    break;
                }
                default:
                    break;
            }
        }
        
    }
    if(currentMonster->isDead()){
    	cout << "You have gained " <<(gamePtr->getCurrentPlayer()->getPlayerStats().getExp() - initialxp) << " experience!\n";
    }
    if(gamePtr->getCurrentPlayer()->getPlayerStats().getLevel() > initiallevel){
    	cout << "You have gained a level! You are now level " << gamePtr->getCurrentPlayer()->getPlayerStats().getLevel()
    			<< endl << "Your stats have increased!\n";
    }
    if (!gamePtr->getCurrentPlayer()->isDead()) {
        update();
    }
    
}
void TextInterface::dropAll(vector<Listing>const * playerInventory) {
    
    bool validResponse = false;
    
    string interfacePrompt = "Do you really wish to drop all your inventory?";
    
    while (!validResponse) {
        
        string response;
        
        getResponse(interfacePrompt, response);
        
        if (response == "y" || response == "yes") {
            
            // drop all
            
            long itemsInInventory = playerInventory->size();
            
            for (long i = 0; i < itemsInInventory; i++) {
                
                dropObject(playerInventory->at(0).objectType, playerInventory->at(0).objectID);
            }
            
            validResponse = true;
            
            update("As you wish, you dropped all your inventory");
            
        }
        
        else if (response == "n" || response == "no") {
            
            // don't drop all
            
            validResponse = true;
            
            update("A wise decision!");
        }
        
        else {
            
            interfacePrompt = "Invalid response, please enter y/yes or n/no\n\nDo you really wish to drop all your inventory?";
            
        }
    }
    
}

void TextInterface::saveGame()
{
    string response;
    string dir;
    bool isSavedGames;
    getResponse("\nDo you want to save this to an existing file? (yes/no/cancel): ", response);
    if (response == "yes" || response == "Yes" || response == "y" || response == "Y") {
        vector<string> gameList;
        gameList.clear();
        gameList = gamePtr->getGameList(isSavedGames);
        if(isSavedGames)
        {
            for(int i = 0; i < gameList.size(); ++i)
            {
                cout << i + 1 << ") " << gameList.at(i) << endl;
            }
            cout << endl << endl;
            getResponse("\nChoose a file to save to: ", response);
            
            if(find_if(response.begin(), response.end(), Utility::charIsNotNumeric) == response.end())
            {
                int responseIndex = atoi(response.c_str());
                if(responseIndex > 0 && responseIndex <= gameList.size())
                {
                    gamePtr->saveGame(gameList.at(responseIndex - 1), false);
                }
            }
        }
        else
        {
            printErrorMsg("Sorry, there are no saved games!");
            saveGame();
        }
    }
    else if (response == "no" || response == "No" || response == "n" || response == "N") {
        getResponse("\nEnter a name to save to: ", response);
        dir = response;
        if(Utility::isValidFileName(dir))
        {
            if(Utility::createDir(dir))
            {
                getResponse("\nThis file already exists.\nAre you sure you want to over write it? (yes/no/cancel): ", response);
                if (response == "yes" || response == "Yes" || response == "y" || response == "Y")
                {
                    gamePtr->saveGame(dir, false);
                }
                else if (response == "no" || response == "No" || response == "n" || response == "N")
                {
                    saveGame();
                }
                else if(response == "cancel" || response == "Cancel")
                {
                    update();
                }
            }
            else
            {
                gamePtr->saveGame(dir, true);
            }
            
        }
        else
        {
            printErrorMsg("You must enter a valid file name, no \'\\\' or \'/\'");
            saveGame();
        }
    }
    else if(response == "cancel" || response == "Cancel")
    {
        update();
    }
    else saveGame();
    
}

void TextInterface::printErrorMsg(string msg)
{
    cout << msg << endl;
}


bool TextInterface::executeBattleCommand(std::string commandToParse, std::string & error) {
    
    commandToParse = Utility::removeGarbage(commandToParse);
    string response;
    if (commandToParse.size() > 0) {
        commandToParse = Utility::lowerCase (commandToParse);
        
        
        vector<string> commands;
        
        Utility::getCommands(commandToParse, commands);
        
        if (commands[0] == "use" || commands[0] == "drink" || commands[0] == "dequip" || commands[0] == "equip") {
            
            if ( commands[0] == "equip" ) {
                
                if (commands.size() > 1) {
                    
                    string intError;
                    if (equipObject(commands[1], intError)) {
                        
                        error = "You equiped the ";
                        error += intError;
                        return true;
                    }
                    else {
                        error = intError;
                        return false;
                    }
                }
            }
            else if ( commands[0] == "dequip" ) {
                
                if (commands.size() > 1) {
                    
                    string intError;
                    if (dequipObject(commands[1], intError)) {
                        
                        error = "You dequiped the ";
                        error += intError;
                        error += " and put it in your inventory";
                        return true;
                    }
                    else {
                        error = intError;
                        return false;
                    }
                }
            }
            
            else if ( commands[0] == "use" || commands[0] == "drink") {
                
                if (commands.size() > 1) {
                    
                    string intError;
                    if (usePotion(commands[1], intError)) {
                        
                        string error = "You drank the ";
                        error += intError;
                        return true;
                    }
                    else {
                        error = intError;
                        return false;
                    }
                    
                }
                
            }
            
            
        }
        else if ( commands[0] == "show" ) {
            
            if (commands.size() > 1 && commands[1] == "inventory") {
                string intError;
                if(showPlayerInventory(gamePtr->getCurrentPlayer()->inventory.getManifest(), intError)) {
                    return true;
                }
                else {
                    error = intError;
                    return false;
                }
            }
            else {
                
                error = "What do you want me to show?";
                return false;
                
            }
        }
        else if ( commands[0] == "i" || commands[0] == "inv" || commands[0] == "inventory") {
            
            
            string intError;
            if(showPlayerInventory(gamePtr->getCurrentPlayer()->inventory.getManifest(), intError)) {
                return true;
            }
            else {
                error = intError;
                return false;
            }
        }
        
        else {
            error = "Sorry I do not understand you or your command is not available in battle mode";
            return false;
        }
    }
    
}
void TextInterface::executeCommand(std::string commandToParse) {
    
    // remove garbage from end of string
    commandToParse = Utility::removeGarbage(commandToParse);
    string response;
    if (commandToParse.size() > 0) {
        commandToParse = Utility::lowerCase (commandToParse);
        cout << commandToParse << endl;
        
        vector<string> commands;
        
        Utility::getCommands(commandToParse, commands);
        Direction ddirection;
        string id;
        
        if (commands[0] == "go" || commands[0] == "move")
        {
            if (commands.size() > 1)
            {
            	string d = commands[1];
                
            	if ( d == "n" || d == "north" )
            		move("north");
            	else if( d == "s" || d == "south" )
            		move("south");
            	else if( d == "w" || d == "west" )
            		move("west");
            	else if( d == "e" || d == "east" )
            		move("east" );
                else
                	update("Invalid direction.  Please try north, west, east, or south.");
            }
            else
            	update("What direction would you like to go?");
        }
        // Make it easier to move, or more user friendly
        else if(commands[0] == "n" || commands[0] == "north" ||
                commands[0] == "s" || commands[0] == "south" ||
                commands[0] == "e" || commands[0] == "east" ||
                commands[0] == "w" || commands[0] == "west") {
            
            if (commands[0].size() > 1) {
                move(commands[0]);
            }
            else if (commands[0] == "n") {
                move("north");
            }
            else if (commands[0] == "s") {
                move("south");
            }
            else if (commands[0] == "w") {
                move("west");
            }
            else if (commands[0] == "e") {
                move("east");
            }
            
        }
        
        else if(((commands[0] == "unlock" && commands.size() > 1) && commands[1] == "chest") || commands[0] == "chest")
        {
            update("This command has not yet been implemented");
        }
        
        else if(((commands[0] == "unlock" && commands.size() > 1) && commands[1] == "door") || commands[0] == "door")
        {
            getResponse("Which direction would you like to use the key? ", response);
            
            unlockDoorCommand(response);
        }
        
        else if((commands[0] == "unlock" && commands.size() > 1) &&
                (commands[1] == "n" || commands[1] == "north" ||
                 commands[1] == "e" || commands[1] == "east" ||
                 commands[1] == "s" || commands[1] == "south" ||
                 commands[1] == "w" || commands[1] == "west"))
        {
            
            unlockDoorCommand(commands[1]);
        }
        
        else if(((commands[0] == "use" && commands.size() > 1) && commands[1] == "key") ||
                commands[0] == "unlock" || commands[0] == "key")
        {
            
            getResponse("What would you like to unlock? ", response);
            executeCommand(response);
        }
        
        //check N,E,W,S, north, east west, south
        else if (  ((commands[0] == "pick" && commands.size() > 1) && commands[1] == "up" )
                 || commands[0] == "get" || commands[0] == "take" )
        {
            int thing = 0;
            if (commands[0] == "pick" && commands[1] == "up")
            {
                thing = 2; // because pick up is 2 word ==> pick up key ==> key will be in the third index in command[]
            }
            else
            {
                thing = 1; //get key, take key , take something
            }
            if (commands.size() > thing) {
                pickUp(commands[thing]); //get key, take key , take something
            }
            else
            {
                update("What are you trying to say?");
            }
        }
        
        // Let user drop something out of his inventory
        else if (commands[0] == "drop") {
            
            vector<Listing>const * playerInventory = gamePtr->getCurrentPlayer()->inventory.getManifest();
            if (commands.size() == 1) {
                // ***TODO create code for prompt in Interface class
                // *** method should be : void getResponse(std::string interfacePrompt, std::string &response);
                
                dropAll(playerInventory);
                
            }
            else {
                
                if (commands[1] == "all")
                {
                    dropAll(playerInventory);
                }
                else{
                    dropItem(commands[1]);
                }
                
            }
        }
        else if ( commands[0] == "show" ) {
            
            if (commands.size() > 1 && commands[1] == "inventory") {
                string intError;
                showPlayerInventory(gamePtr->getCurrentPlayer()->inventory.getManifest(), intError);
                update(intError);
            }
            else {
                
                update("What do you want me to show?");
            }
        }
        else if ( commands[0] == "i" || commands[0] == "inv" || commands[0] == "inventory") {
            
            string intError;
            showPlayerInventory(gamePtr->getCurrentPlayer()->inventory.getManifest(), intError);
            update(intError);
        }
        else if ( commands[0] == "h" || commands[0] == "?" || commands[0] == "help" ) {
            
            //vector<std::string> * helpFile;
            
            showHelp(gamePtr->loadHelp());
            update();
            
        }
        
        else if ( commands[0] == "attack" || commands[0] == "kill" || commands[0] == "k" ) {
            
            if (commands.size() > 1) {
                attack(commands[1]);
                update();
                
            }
            else {
                
                update("You swing your sword aimlessly at the air and fall on your head!");
            }
            
            
        }
        else if ( commands[0] == "equip" ) {
            
            if (commands.size() > 1) {
                
                string error;
                if (equipObject(commands[1], error)) {
                    
                    string msg = "You equiped the ";
                    msg += error;
                    update(msg);
                }
                else {
                    update(error);
                }
            }
        }
        else if ( commands[0] == "dequip" ) {
            
            if (commands.size() > 1) {
                
                string error;
                if (dequipObject(commands[1], error)) {
                    
                    string msg = "You dequiped the ";
                    msg += commands[1];
                    msg += " and put it in your inventory";
                    update(msg);
                }
                else {
                    update(error);
                }
            }
        }
        
        else if ( commands[0] == "use" || commands[0] == "drink") {
            
            if (commands.size() > 1) {
                
                string error;
                if (usePotion(commands[1], error)) {
                    
                    string msg = "You drank the ";
                    msg += error;
                    stringstream currentstats;
                    currentstats << "\nCurrent HP: " << gamePtr->getCurrentPlayer()->getPlayerStats().getHitPoints();
                    currentstats << "\nCurrent AP: " << gamePtr->getCurrentPlayer()->getPlayerStats().getActionPoints() << endl;
                    msg += currentstats.str();
                    update(msg);
                }
                else {
                    update(error);
                }
                
            }
            
        }
        
        else if ( commands[0] == "q" || commands[0] == "quit" || commands[0] == "exit") {
            
            bool validResponse = false;
            
            string interfacePrompt = "\nDo you really wish to quit? ";
            
            while (!validResponse) {
                
                
                getResponse(interfacePrompt, response);
                Utility::lowerCase(response);
                if (response == "y" || response == "yes") {
                    
                    if (numberOfQuitRequests < 2) {
                        
                        stringstream msg;
                        
                        string msgPrefix = "Sorry ";
                        
                        string msgPostfix = ", you can never leave Dungeons and Princesses!";
                        
                        msg << msgPrefix << gamePtr->getCurrentPlayer()->getDesc() << msgPostfix;
                        
                        numberOfQuitRequests++;
                        update(msg.str());
                    }
                    else {
                        getResponse("\nDo you wish to save the game? (yes/no/cancel) ", response);
                        Utility::lowerCase(response);
                        if(response.compare("yes") == 0 ||
                           response.compare("y") == 0 )
                            saveGame();
                        else if(response.compare("no") == 0 ||
                                response.compare("n") == 0 )
                            exit(0);
                        else update();
                    }
                    validResponse = true;
                    
                    
                }
                
                else if (response == "n" || response == "no") {
                    
                    // don't drop all
                    
                    validResponse = true;
                    
                    update("A wise decision!");
                }
                
                else {
                    
                    interfacePrompt = "Invalid response, please enter y/yes or n/no\n\nDo you really wish quit?";
                    
                }
            }
            
        }
        else {
            
            update("What are you trying to say?");
        }
    }
    else {
        update("What did you say???");
    }
}

void TextInterface::unlockDoorCommand(string direction)
{
    if (direction.size() > 1) {
        unlock(direction);
    }
    else if (direction == "n") {
        unlock("north");
    }
    else if (direction == "s") {
        unlock("south");
    }
    else if (direction == "w") {
        unlock("west");
    }
    else if (direction == "e") {
        unlock("east");
    }
    else {
        update("Sorry I do not understand");
    }
}
